package com.yimi.yinhepda.utils;

import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author 000432
 */
public class DateCommom {

  /**
   * 根据年 月 获取对应的月份 天数
   */
  public static int getDaysByYearMonth(int year, int month) {

    Calendar a = Calendar.getInstance();
    a.set(Calendar.YEAR, year);
    a.set(Calendar.MONTH, month - 1);
    a.set(Calendar.DATE, 1);
    a.roll(Calendar.DATE, -1);
    int maxDate = a.get(Calendar.DATE);
    return maxDate;
  }

  /**
   * 根据当年的年份
   */
  public static int getNowDateYear() {
    Calendar cal = Calendar.getInstance();
    int year = cal.get(Calendar.YEAR);
    return year;
  }

  /**
   * 根据当年的月份
   */
  public static int getNowDateMonth() {
    Calendar cal = Calendar.getInstance();
    //首位是0  ，月份需要+1
    int month = cal.get(Calendar.MONTH) + 1;
    return month;
  }

  /**
   * 根据当年的日
   */
  public static int getNowDateDay() {
    Calendar cal = Calendar.getInstance();
    //首位是0  ，月份需要+1
    int day = cal.get(Calendar.DATE);
    return day;
  }

  public static String getTwoDigit(int value) {
    String reString = "";
    if (value >= 0 && value < 10) {
      reString = "0" + value;
    } else {
      reString = value + "";
    }
    return reString;
  }

  /**
   * 获取是周几
   */
  public static String getWeekDay() {
    String DateStr = "";
    Calendar c = Calendar.getInstance();
    int weekDay = c.get(Calendar.DAY_OF_WEEK) - 1;
    if (weekDay == 1) {
      DateStr += "周一";
    } else if (weekDay == 2) {
      DateStr += "周二";
    } else if (weekDay == 3) {
      DateStr += "周三";
    } else if (weekDay == 4) {
      DateStr += "周四";
    } else if (weekDay == 5) {
      DateStr += "周五";
    } else if (weekDay == 6) {
      DateStr += "周六";
    } else {
      DateStr += "周日";
    }
    return DateStr;
  }

  public static String getTime(String returnTimeType) {
    SimpleDateFormat dateFormat = new SimpleDateFormat(returnTimeType);
    String time = null;
    try {
      Date now = new Date();
      time = dateFormat.format(now);
    } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
    }
    return time;
  }

  public static long getLongTime() {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
    long time = 0;
    try {
      Date now = new Date();
      time = Long.parseLong(dateFormat.format(now));
    } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
    }
    return time;
  }

  public static long getLongDate() {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    long time = 0;
    try {
      Date now = new Date();
      time = Long.parseLong(dateFormat.format(now));
    } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
    }
    return time;
  }

  public static long getLongTime(Date date) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
    long time = 0;
    try {
      time = Long.parseLong(dateFormat.format(date));
    } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
    }
    return time;
  }

  /***
   * 写入文件，例如定改1.txt ,追加的方式写入
   */
  public static void appendTextToFile(String filePath, String conent) {
    FileWriter writer = null;
    try {
      // 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
      writer = new FileWriter(filePath, true);
      writer.write(conent);
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if (writer != null) {
          writer.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

  public static String getTime(String requestDate, String requestTimeType, String returnTimeType) {//参数1、传进的时间；2、传进时间的格式；3、返回时间格式
    SimpleDateFormat dateFormatRequest = new SimpleDateFormat(requestTimeType);
    SimpleDateFormat dateFormatReturn = new SimpleDateFormat(returnTimeType);
    String time = null;
    try {
      Date date = dateFormatRequest.parse(requestDate);
      time = dateFormatReturn.format(date);
    } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
    }
    return time;
  }
}
